package com.gome.axwl.aoc.web.controller.file;

import com.cloudzone.cloudoss.api.open.OSS;
import com.gome.axwl.aoc.common.util.XskyS3Util;
import com.gome.axwl.aoc.entity.dto.FileUploadRecordDto;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Api(tags = "文件上传与下载")
@RequestMapping("file")
@RestController
public class UploadController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private OSS oss;

    @ApiOperation(value = "上传图片保存到OSS")
    @RequestMapping(value="/image", method = RequestMethod.POST)
    public ResultData<String> image(@RequestParam("image") MultipartFile image) throws Throwable {
        String uri = oss.putObject(UUID.randomUUID().toString()+".jpg", image.getInputStream());
        System.out.println("oss  ---->  " + uri);
        return ResultData.newResultData(uri);
    }


    @ApiOperation(value = "上传文件", notes = "development by chenjunjie")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", value = "文件名称", paramType = "query",dataType="String"),
    })
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ResultData<FileUploadRecordDto> uploadContractAttachment(
            @RequestParam("file") MultipartFile file,
            @RequestParam(name = "fileName", required = false) String fileName){
        //限制文件大小不大于5M
        if (file!=null && file.getSize()>5242880){
            return new ResultData<>(ErrorCode.FAILOR,"文件大小超过5M，上传失败");
        }

        String riginalFilename = file.getOriginalFilename();
        String postfix = riginalFilename.substring(riginalFilename.lastIndexOf(".") + 1);
        String uuidStr = UUID.randomUUID().toString().replace("-", "");
        fileName = uuidStr +"." +postfix;

        ResultData resultData = new ResultData<>();
        // 调用接口，传入file流，获取fileUrl
        String downLoadUrl = XskyS3Util.uploadAndReturnUrl(file,null, fileName);
        downLoadUrl = downLoadUrl.replaceAll("bjgc3.gos.gomedc.com","bjgc3-gos.gomecloud.com");
        try {
            Map resultMap = new HashMap();
            resultMap.put("fileUrl",downLoadUrl);
            resultMap.put("fileName",riginalFilename);
            resultData.setData(resultMap);
            return resultData;
        } catch (Exception e) {
            logger.error("上传文件失败--error:{}", ExceptionUtils.getFullStackTrace(e));
            resultData.setCode(ErrorCode.QUERY_FAILOR);
            resultData.setMessage(ErrorCode.QUERY_FAILOR_MSG);
            return resultData;
        }
    }
}
